from urllib import urlopen
import simplejson
from lxml import etree

# The google translate API can be found here:
# http://code.google.com/apis/ajaxlanguage/documentation/#Examples
def translate(text, sourceLang = 'en', targetLang = 'pt'):
    try:
        url = "https://www.googleapis.com/language/translate/v2?" + \
              "key=AIzaSyARZORpTevzPWt1XhsIkJUQ0PVZlK4cYTk&q=%s&source=%s&target=%s" % (text, sourceLang, target)
        search_results = urlopen(url)
        json = simplejson.loads(search_results.read())
        return json['data']['translations'][0]['translatedText']
    except:
        return ''

def webPageRead(urlLink):
    # Get a HTML web page to read
    page = urlopen(urlLink)
    # Read from the object, storing the page's contents in 'text'.
    text = page.read()
    page.close()
    return text

print webPageRead('http://shakespeare.mit.edu/hamlet/hamlet.1.1.html')
